package com.doraz.orb.ui;

import java.util.ArrayList;

import com.doraz.orb.Constants;
import com.doraz.orb.R;
import com.doraz.orb.methods.file.Browse;
import com.doraz.orb.structs.Group;
import com.doraz.orb.structs.OrbItem;
import com.doraz.orb.structs.OrbObject;
import com.doraz.orb.structs.SearchResult;
import com.doraz.orb.structs.User;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html.TagHandler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class BrowseFiles extends ListActivity {
	private final static String TAG = "BrowseFiles";
	
	public final static int INTENT_BROWSE_FOLDER = 1;
	public final static int RESULT_OK = 2;
	public final static int RESULT_ERROR = 2;
	
	private Bundle extras;
	private ArrayList<OrbObject> objs;
	private User user = null;
	private ListView lv = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle extras = getIntent().getExtras();
		user = (User) extras.get("User");
		objs = (ArrayList<OrbObject>) extras.get("OrbObjects");
		lv = getListView();
		
		//Set the item click listener
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				OrbObject object = (OrbObject) parent.getItemAtPosition(position);
				selectItem(object);
			}			
		});
		
		getListData();
	}
	
	/**
	 * Loads the list and the list adapter
	 */
	private void getListData() {
		if(objs == null) {
			objs = getDefaultList();
		}		
		ArrayAdapter<OrbObject> adapter = new OrbObjectAdapter(this, R.layout.orbobject_layout, objs);
		lv.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}
	
	/**
	 * 
	 * @param object
	 */
	private void selectItem(OrbObject object) {
		try {
			switch(object.getObjectType()) {
			
			case OrbObject.ORB_GROUP: {
				Group group = (Group) object;
				Browse browseMethod = new Browse(group.getPath(), "7");
				if(browseMethod.exectute()) {
					SearchResult result = (SearchResult) browseMethod.getResultObject();
					Intent intent = new Intent(this, BrowseFiles.class);
					intent.putExtra("OrbObjects", result.getResults());
					startActivityForResult(intent, INTENT_BROWSE_FOLDER);
				}
				break;
			}
			
			case OrbObject.ORB_ITEM: {
				break;
			}
			
			case OrbObject.ORB_COMMONMETHOD: {
				break;
			}
			}
		} catch(Exception e) {
			Log.w(TAG, "Could not select item: "+e.getMessage());
		}
	}
	
	/**
	 * Gets a default list of folders to display
	 * 
	 * @return
	 */
	private ArrayList<OrbObject> getDefaultList() {
		ArrayList<OrbObject> folders = new ArrayList<OrbObject>();
		folders.add(new Group(Constants.MYCOMPUTER, "My Computer"));
		folders.add(new Group(Constants.MYDOCUMENTS, "My Documents"));
		folders.add(new Group(Constants.MYMUSIC, "My Music"));
		folders.add(new Group(Constants.MYMUSIC, "My Videos"));
		folders.add(new Group(Constants.NETWORK, "Network"));
		return folders;
	}
}
